package com.qls.ali.biz.data.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qls.ali.model.dto.hotel.request.DelHotel;
import com.qls.ali.model.dto.hotel.request.PushHotel;
import com.qls.ali.model.dto.request.*;
import com.qls.ali.model.dto.rp.request.DelRp;
import com.qls.ali.model.dto.rp.request.PushRp;
import com.qls.core.ali.order.deligoods.request.DeliGoods;
import com.qls.core.dto.HtDailyRoom;
import com.qls.core.entity.HtHotel;
import com.qls.core.entity.HtRoomType;
import com.qls.core.entity.HtSellRoom;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 推送飞猪的工具类
 *
 * @author doyoung
 */
@Slf4j
public class FzUtil {

    /**
     * 更新飞猪订单状态接口
     *
     * @param deliGoods  DeliGoods
     * @param appKey     String
     * @param sessionKey String
     * @param secret     String
     * @return String
     */
    public static String order(DeliGoods deliGoods, String appKey, String sessionKey, String secret) {
        return fzSendComm(deliGoods, "taobao.xhotel.order.update", secret, appKey, sessionKey);
    }

    /**
     * 酒店推送(酒店推送或更新)
     *
     * @param htHotelinfo 酒店实体类
     * @param appKey      飞猪appKey
     * @param sessionKey  飞猪session
     * @param secret      飞猪secret
     * @return String
     */
    public static String pushHotel(HtHotel htHotelinfo, String appKey, String sessionKey, String secret) {
        PushHotel pushHotel = new PushHotel();
        pushHotel.setAddress(htHotelinfo.getAddress());
        pushHotel.setCity(htHotelinfo.getCity());
        pushHotel.setHotelCode(htHotelinfo.getHotelCode().toString());
        pushHotel.setHotelName(htHotelinfo.getHotelName());
        pushHotel.setPhone(htHotelinfo.getPhone());
        pushHotel.setStatus(htHotelinfo.getHotelStatus());
        return fzSendComm(pushHotel, "taobao.xhotel.update", secret, appKey, sessionKey);
    }

    /**
     * 批量推送 rates（最多每次三十条）
     *
     * @param stringListMap Map<String, List<HtDailyRoom>>
     * @param appKey        String
     * @param sessionKey    String
     * @param secret        String
     * @return String
     */
    public static String pushRates(Map<String, List<HtDailyRoom>> stringListMap, String appKey, String sessionKey, String secret) {
        List<Rates> ratesList = new ArrayList<>();
        for (String k : stringListMap.keySet()) {
            String[] newStr = k.split("_");
            String rpCode = newStr[0];
            String roomCode = newStr[1];
            List<HtDailyRoom> htDailyRooms = stringListMap.get(k);
            List<InventoryPrice> inventoryPrices = new ArrayList<>();
            for (HtDailyRoom htDailyRoom : htDailyRooms) {
                InventoryPrice inventoryPrice = new InventoryPrice();
                inventoryPrice.setDate(htDailyRoom.getDate());
                inventoryPrice.setPrice(htDailyRoom.getPrice());
                inventoryPrice.setQuota(htDailyRoom.getSaleNum());
                inventoryPrice.setStatus(htDailyRoom.getStatus());
                inventoryPrices.add(inventoryPrice);
            }
            Data data = new Data();
            data.setInventoryPrices(inventoryPrices);
            Rates rates = new Rates();
            rates.setData(data);
            rates.setRoomCode(roomCode);
            rates.setRpCode(rpCode);
            ratesList.add(rates);
        }
        String ratesListStr = JSONObject.toJSONString(ratesList);
        /*System.out.println(JSONObject.toJSON(ratesList));*/
        RateInventoryPriceMap rateInventoryPriceMap = new RateInventoryPriceMap();
        rateInventoryPriceMap.setRatesList(ratesListStr);
        return fzSendComm(rateInventoryPriceMap, "taobao.xhotel.rates.increment", secret, appKey, sessionKey);
    }

    /**
     * 酒店信息删除 级联删除房型
     *
     * @param hotelCode  酒店编码
     * @param appKey     飞猪appKey
     * @param sessionKey 飞猪session
     * @param secret     飞猪 secret
     * @return String
     */
    public static String delHotel(String hotelCode, String appKey, String sessionKey, String secret) {
        DelHotel delHotel = new DelHotel();
        delHotel.setHotelCode(hotelCode);
        // 调用飞猪接口删除
        return fzSendComm(delHotel, "taobao.xhotel.delete", secret, appKey, sessionKey);
    }

    /**
     * 房型推送
     *
     * @param htRoomType 房型实体类
     * @param appKey     飞猪appKey
     * @param secret     飞猪secret
     * @param sessionKey 飞猪session
     * @return String
     */
    public static String pushRoom(HtRoomType htRoomType, String appKey, String sessionKey, String secret) {
        PushRoom pushRoom = new PushRoom();
        pushRoom.setBedType(htRoomType.getBedType());
        pushRoom.setHotelCode(htRoomType.getHotelCode().toString());
        pushRoom.setRoomCode(htRoomType.getRoomCode().toString());
        pushRoom.setRoomName(htRoomType.getRoomName());
        // 调用飞猪接口推送
        return fzSendComm(pushRoom, "taobao.xhotel.roomtype.add", secret, appKey, sessionKey);
    }

    /**
     * 价格政策推送
     *
     * @param htSellRoom rp实体类
     * @param appKey     飞猪appKey
     * @param sessionKey 飞猪session
     * @param secret     飞猪secret
     * @return String
     */
    public static String pushRp(HtSellRoom htSellRoom, String rpCode, String appKey, String sessionKey, String secret, String channel) {
        PushRp pushRp = new PushRp();
        pushRp.setBreakfastCal(htSellRoom.getBreakfastCal());
        pushRp.setBreakfastCount(htSellRoom.getBreakfastNum());
        pushRp.setCancelPolicy(htSellRoom.getCancelPolicy());
        pushRp.setCancelPolicyCal(htSellRoom.getCancelPolicyCal());
        pushRp.setHotelCode(htSellRoom.getHotelCode().toString());
        pushRp.setMaxAdvHours(htSellRoom.getMaxHours());
        pushRp.setMaxDays(htSellRoom.getMaxDays());
        pushRp.setMinAdvHours(htSellRoom.getMinHours());
        pushRp.setMinDays(htSellRoom.getMinDays());
        pushRp.setRoomCode(htSellRoom.getRoomCode().toString());
        pushRp.setRpCode(rpCode);
        pushRp.setRpName(htSellRoom.getRpName());
        pushRp.setChannel(channel);
        Integer status = htSellRoom.getRpStatus();
        if (Objects.isNull(status)) {
            status = 1;
        }
        if (status != 1) {
            status = 2;
        }
        pushRp.setStatus(status);
        // 调用飞猪接口推送
        return fzSendComm(pushRp, "taobao.xhotel.rateplan.update", secret, appKey, sessionKey);
    }

    /**
     * 价格政策删除 级联删除价格
     *
     * @param rpCode     自定义rp编码
     * @param appKey     飞猪appKey
     * @param sessionKey 飞猪session
     * @param secret     飞猪secret
     * @return String
     */
    public static String delRp(String rpCode, String appKey, String sessionKey, String secret) {
        DelRp delRp = new DelRp();
        delRp.setRpCode(rpCode);
        return fzSendComm(delRp, "taobao.xhotel.rateplan.delete", secret, appKey, sessionKey);
    }

    /**
     * 调用飞猪接口的方法
     *
     * @param o       Object
     * @param secret  String
     * @param appKey  String
     * @param session String
     * @param method  String
     * @return String
     */
    private static String fzSendComm(Object o, String method, String secret, String appKey, String session) {
        String pushData = JSON.toJSONString(o);
        JSONObject jsonObject = (JSONObject) JSONObject.parse(pushData);
        String result;
        try {
            result = FzSdk.execute(method, jsonObject, secret, appKey, session);
        } catch (Exception e) {
            result = e.getMessage();
        }
        return result;
    }
}

